package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.trade.adapter.ChooseBankAdapter;
import com.sanpu.kxe.business.trade.adapter.FastPayBannerHolder;
import com.sanpu.kxe.business.trade.bean.FastPayBannerOutput;
import com.sanpu.kxe.business.trade.contract.FastPayComfirmContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.presenter.FastPayComfirmPresenter;
import com.sanpu.kxe.common.enums.PayType;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.banner.MZBannerView;
import com.sanpu.kxe.module.banner.holder.MZHolderCreator;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SystemBarHelper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Description：快捷native支付
 * Created by 薛勇军 on 2019/3/7.
 */

public class FastPayComfirmActivity extends BaseTitleActivity implements FastPayComfirmContract.View {

    @Bind(R.id.arrivalAmountTv)
    TextView vArrivalAmountTv;
    @Bind(R.id.payment_amount)
    TextView vPaymentAmount;
    @Bind(R.id.out_icon)
    ImageView vOutIcon;
    @Bind(R.id.out_bank_name)
    TextView vOutBankName;
    @Bind(R.id.out_select_bank)
    LinearLayout vOutSelectBank;
    @Bind(R.id.in_icon)
    ImageView vInIcon;
    @Bind(R.id.in_bank_name)
    TextView vInBankName;
    @Bind(R.id.in_select_bank)
    LinearLayout vInSelectBank;
    @Bind(R.id.phone_hint)
    ImageView vPhoneHint;
    @Bind(R.id.tvPhoneNumber)
    TextView tvPhoneNumber;
    @Bind(R.id.etSendCode)
    EditTextWithDel etSendCode;
    @Bind(R.id.sendCodeRtv)
    RoundTextView sendCodeRtv;
    @Bind(R.id.comfirmRtv)
    RoundTextView comfirmRtv;
    @Bind(R.id.banner)
    MZBannerView vMZBannerView;
    @Bind(R.id.remarkTv)
    TextView remarkTv;
    @Bind(R.id.headerView)
    RelativeLayout headerView;

    private ChooseBankAdapter mBankAdapter;

    private FastPayComfirmContract.Presenter mPresenter;

    private ArrayList<BankInfoOutput> mOutBankList = new ArrayList<>();
    private ArrayList<BankInfoOutput> mInBankList = new ArrayList<>();

    private String mOutBankId, mInBankId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_fast_pay_comfirm;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);

        mBankAdapter = new ChooseBankAdapter(mContext, new ArrayList<BankInfoOutput>());
        try {
            String mPaymentAmt = getIntent().getStringExtra(TradeConfig.INTENT_REQAMOUNT);
            double rate = getIntent().getDoubleExtra(TradeConfig.INTENT_RATE, 0);
            int stlType = getIntent().getIntExtra(TradeConfig.INTENT_STLTYPE, 0);
            String rlId = getIntent().getStringExtra(TradeConfig.INTENT_RLID);
            String needPic = getIntent().getStringExtra(TradeConfig.INTENT_NEEDPIC);
            String maxFeeAmt = getIntent().getStringExtra(TradeConfig.INTENT_MAXFEEAMT);
            String otherFeeAmt = getIntent().getStringExtra(TradeConfig.INTENT_OTHERFEEAMT);
            if (!TextUtils.isEmpty(mPaymentAmt))
                vPaymentAmount.setText(new DecimalFormat("#0.00").format(new Double(mPaymentAmt)));
            double needFee = new Double(mPaymentAmt) * rate / 100;
            if (!TextUtils.isEmpty(maxFeeAmt)) {
                double maxFee = Double.parseDouble(maxFeeAmt);
                if (maxFee > 0 && maxFee < needFee) {
                    needFee = maxFee;
                }
            }
            double otherFee = 0.00;
            if (!TextUtils.isEmpty(otherFeeAmt)) {
                otherFee = Double.parseDouble(otherFeeAmt);
            }
            double arrivalAmount = new Double(mPaymentAmt) - needFee - otherFee;
            if (stlType == 1) {
                remarkTv.setText("勿需提现，T+1到账");
            } else {
                remarkTv.setText("勿需提现，即刷即到；每笔收取" + otherFee + "元/笔");
            }
            vArrivalAmountTv.setText(new DecimalFormat("#0.00").format(arrivalAmount));
            PayType mType = (PayType) getIntent().getSerializableExtra(TradeConfig.INTENT_REQTYPE);

            mPresenter = new FastPayComfirmPresenter(this);
            mPresenter.initData(needPic, rlId, mType.getValue(), mPaymentAmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showBankInfo(BankInfoOutput infoOutput, boolean isOutBank) {
        try {
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl() + infoOutput.getLogoPath(), isOutBank ? vOutIcon : vInIcon);
            String bankNo = infoOutput.getBankNo();
            (isOutBank ? vOutBankName : vInBankName).setText(infoOutput.getBankName().replace(" ", "")
                    + "  (" + bankNo.substring(bankNo.length() - 4, bankNo.length()) + ")");
            (isOutBank ? vOutBankName : vInBankName).setTag(infoOutput);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isOutBank) {
            mOutBankId = infoOutput.getBankId();
            tvPhoneNumber.setText(infoOutput.getRsdTel() == null ? "" : infoOutput.getRsdTel());
        } else {
            mInBankId = infoOutput.getBankId();
        }
    }

    @Override
    public void setBankListData(List<BankInfoOutput> outBankList, List<BankInfoOutput> inBankList) {
        mOutBankList.clear();
        mInBankList.clear();
        if (outBankList != null) {
            mOutBankList.addAll(outBankList);
        }
        if (inBankList != null) {
            mInBankList.addAll(inBankList);
        }
        if (!mOutBankList.isEmpty()) {
            mPresenter.quickReportSelect(mOutBankList.get(0));
        }
        if (vInBankName.getTag() == null) {
            showBankInfo(mInBankList.get(0), false);
        }
    }

    @Override
    public void showBannerView(List<FastPayBannerOutput> bannerOutputs) {
        if (bannerOutputs == null || bannerOutputs.size() == 0) {
            vMZBannerView.setVisibility(View.GONE);
            return;
        }
        vMZBannerView.setVisibility(View.VISIBLE);
        vMZBannerView.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
                FastPayBannerOutput bannerOutput =
                        (FastPayBannerOutput) vMZBannerView.getAdapter().getItem(position);
                mPresenter.onBannerItemClick(bannerOutput);
            }
        });
        vMZBannerView.setPages(bannerOutputs, new MZHolderCreator<FastPayBannerHolder>() {
            @Override
            public FastPayBannerHolder createViewHolder() {
                return new FastPayBannerHolder();
            }
        });
        if (bannerOutputs.size() > 1)
            vMZBannerView.start();
    }

    /**
     * 银行卡变更，重新请求
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RefreshBankEvent event) {
        if (event != null) {
            mPresenter.selectUrmBankPage();
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .keyboardEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(FastPayComfirmContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
